package com.serial.comm.config.rabbitmq;

import com.alibaba.fastjson.JSON;
import com.serial.comm.core.constants.RabbitConst;
import com.serial.comm.core.utils.ValueUtils;
import com.serial.comm.modular.serialPort.model.FodbMsg;
import com.serial.comm.modular.serialPort.model.TelegramMQ;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.UUID;

@Component
public class SerialMsgSender {

    @Resource
    private RabbitTemplate rabbitTemplate;


    /**
     * 向fodb系统 发送消息
     */
    public void sendFodbReadQueueMsg(FodbMsg fodbMsg) {
        rabbitTemplate.convertSendAndReceive(RabbitConst.QUEUE_FODB_READ, JSON.toJSONString(fodbMsg));
        ;
    }

    /**
     * 向串口读取 原始电报 队列发送消息
     */
    public void sendReadBaseQueueMsg(String msg) {
        String messageId = ValueUtils.parseString(UUID.randomUUID());
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setMessageId(messageId);
        Message message = new Message(msg.getBytes(), messageProperties);
        rabbitTemplate.convertSendAndReceive(RabbitConst.BUSINESS_EXCHANGE, RabbitConst.QUEUE_ROUTING_KEY_BASE_READ, message);
    }

    /**
     * 向串口读取 单一电报 队列发送消息
     */
    public void sendReadUnitaryQueueMsg(String msg) {
        String messageId = ValueUtils.parseString(UUID.randomUUID());
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setMessageId(messageId);
        Message message = new Message(msg.getBytes(), messageProperties);
        rabbitTemplate.convertSendAndReceive(RabbitConst.BUSINESS_EXCHANGE, RabbitConst.QUEUE_ROUTING_KEY_UNITARY_READ, message);
    }

    /**
     * 向串口业务写入队列发送消息
     */
    public void sendWriteQueueMsg(TelegramMQ telegramMQ) {

        rabbitTemplate.convertSendAndReceive(RabbitConst.BUSINESS_EXCHANGE, RabbitConst.QUEUE_ROUTING_KEY_WRITE, telegramMQ);
    }

    /**
     * 向串口写入 重试队列发送消息
     */
    public void sendWriteRetryQueueMsg(TelegramMQ telegramMQ) {

        rabbitTemplate.convertSendAndReceive(RabbitConst.BUSINESS_EXCHANGE_RETRY, RabbitConst.QUEUE_ROUTING_KEY_WRITE_RETRY, telegramMQ);
    }


}
